package ygw.study.advanced.reentrantLock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 
 * </p>
 * <p>
 * Copyright:.All rights reserved.
 * </p>
 * <p>
 * Company:Zsoft
 * </p>
 * <p>
 * CreateDate: 2021-07-04
 * </p>
 * 
 * @author YuGongWen
 * @history Mender: YuGongWen；Date: 2021-07-04；
 */
public class ReentrantLockTest extends Thread {

    public static ReentrantLock lock = new ReentrantLock();
    public static int i = 0;

    public ReentrantLockTest(String name) {
        super.setName(name);
    }

    @Override
    public void run() {
        lock.lock();
        try {
            for (int j = 0; j < 100; j++) {
                System.out.println(this.getName() + " " + i);
                i++;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
        
        
        
        
        
    }

    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        ReentrantLockTest test1 = new ReentrantLockTest("thread11111111111111111");
        ReentrantLockTest test2 = new ReentrantLockTest("thread2");

        test1.start();
        test2.start();
        test1.join();
        test2.join();
        System.out.println(i);
    }
}
 